﻿//---------------------------------------------------------------------------
// 
// File: HtmlTokenType.cs
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// Description: Definition of token types supported by HtmlLexicalAnalyzer
//
//---------------------------------------------------------------------------

namespace HTMLConverter.XamlConverters
{
    using System.Xml.Linq;
    using HTMLConverter.Xml;
    using System.Collections.Generic;

    /// <summary>
    /// HtmlToXamlConverter is a static class that takes an HTML string
    /// and converts it into XAML
    /// </summary>
    internal class TableRowConverter : IConverter
    {
        const int TableWidth = 480;

        public Tag Type
        {
            get { return Tag.TableRow; }
        }

        public XmlElement Convert(XNode htmlNode)
        {
            List<XmlElement> cells = new List<XmlElement>();
            if (htmlNode is XmlElement)
            {
                XmlElement htmlElement = htmlNode as XmlElement;
                for (XNode htmlChildNode = htmlElement.FirstNode; htmlChildNode != null; htmlChildNode = htmlChildNode.NextNode)
                {
                    if (htmlChildNode is XmlElement)
                    {
                        XmlElement htmlChildElement = (XmlElement)htmlChildNode;
                        switch (htmlChildElement.LowerName)
                        {
                            case "td":
                            case "th":
                                XmlElement cell = Converters.Collection[Tag.TableCell].Convert(htmlChildElement);
                                if (cell != null)
                                {
                                    cells.Add(cell);
                                }
                                break;
                        }
                    }
                }
            }

            if (cells.Count > 0)
            {
                XmlElement row = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);

                int cellsWidth = TableWidth / cells.Count;
                foreach (XmlElement cell in cells)
                {
                    // cell.SetAttribute(HtmlToXaml.Xaml_Width, cellsWidth.ToString());
                    row.AppendChild(cell);
                }

                return row;
            }
            else
            {
                return null;
            }
        }
    }
}
